package model.viewModels;

import java.util.ArrayList;
import java.util.List;
import model.Match;
import model.Move;
import view.ChessFrame;
import view.viewPanels.ArchGamesViewPanel;
import control.Communication;

/**
 * model-class for the actGames-view
 */
public class ArchGamesModel {

    /**
     * related view-class
     */
    private ArchGamesViewPanel archGamesViewPanel;

    /**
     * List of all archived matches
     */
    private ArrayList<Match> archMatches;

    /**
     * the actual match
     */
    private Match actualMatch;

    /**
     * constructor of the ActGamesModel
     * 
     * @param archGamesView
     *            the related view class
     */
    public ArchGamesModel( ArchGamesViewPanel archGamesView )
    {
	this.archGamesViewPanel = archGamesView;
    }

    /**
     * updates the match lists on login and every 5 minutes
     */
    public final void updateArchMatchData()
    {
	this.archMatches = ChessFrame.centerViewPanel.getCenterModel().getArchMatches();
	this.archGamesViewPanel.updateMatchTable( this.archMatches );
    }

    /**
     * sets the actual match attribute
     * 
     * @param selected
     *            the selected row in actGames table
     */
    public final void setActualMatch( int selected )
    {
	if ( this.actualMatch != null )
	{
	    this.actualMatch.resetFigureSelection();
	}

	this.actualMatch = this.archMatches.get( selected );

	loadActMoves();
    }

    /**
     * load the moves for the actual match
     */
    private void loadActMoves()
    {
	// get move data
	String[] moveData = Communication.getArchMoves( this.actualMatch.getId(), CenterModel.loggedInUser.getNick() );

	// build move list
	List<Move> moves = new ArrayList<Move>();
	String[] data;
	Move move;
	String one = "1";
	for ( int i = 0; i < moveData.length; i++ )
	{
	    data = moveData[i].split( "#" );
	    move = new Move();
	    move.setNumber( Integer.valueOf( data[0] ) );
	    move.setDate( data[1] );
	    move.setStartField( data[2] );
	    move.setEndField( data[3] );
	    move.setCastling( data[4].equals( one ) );
	    move.setChecked( data[5].equals( one ) );
	    move.setCheckmate( data[6].equals( one ) );
	    move.setEnPassant( data[7].equals( one ) );
	    moves.add( move );
	}
	this.actualMatch.setMovesList( moves );
	this.actualMatch.updateFigurePositions();

	// prepare data for moves table
	String[][] values = new String[moves.size()][4];
	for ( int i = 0; i < values.length; i++ )
	{
	    values[i][0] = String.valueOf( moves.get( i ).getNumber() );
	    values[i][1] = moves.get( i ).getStartField() + " - " + moves.get( i ).getEndField();
	    values[i][2] = this.actualMatch.getActingPlayer( moves.get( i ).getNumber() );
	    values[i][3] = moves.get( i ).getFormattedDate();
	}

	// update moves table
	this.archGamesViewPanel.getCtMoves().updateValues( values );
	if ( values.length > 0 )
	{
	    this.archGamesViewPanel.getCtMoves().setSelectedExternal( 0 );
	}
    }

    /**
     * getter of actualMatch
     * 
     * @return the actualMatch
     */
    public final Match getActualMatch()
    {
	return this.actualMatch;
    }

    /**
     * getter of archGamesViewPanel
     * 
     * @return the archGamesViewPanel
     */
    public final ArchGamesViewPanel getArchGamesViewPanel()
    {
	return this.archGamesViewPanel;
    }

    /**
     * getter of archMatches
     * 
     * @return the archMatches
     */
    public final ArrayList<Match> getArchMatches()
    {
	return this.archMatches;
    }

}